package com.yatush.prepare.jsp;

import java.io.ByteArrayInputStream;
import java.io.CharConversionException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;

import org.apache.commons.digester.RegexMatcher;
import org.apache.commons.digester.RegexRules;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.hsqldb.lib.StringInputStream;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;

public class JspReader {

	private static final String PREFIX_DELIMITER = ":"; 
	private static final String PREFIX_REPLACEMENT = "x"; 
	
	String header;
	String body;

	public JspReader(File file) throws SAXException, DocumentException, IOException {
		header = readJspHeader(file);
		body = readAndPreProcessBody(file);
		System.out.println(header);
		System.out.println("---------------------");
		System.out.println(body);
	}

	private String readAndPreProcessBody(File file) throws IOException {
		FileReader fr = new FileReader(file);
		fr.skip(header.length());
		while(fr.read()!='<');
		StringBuilder sb = new StringBuilder();
		sb.append('<');
		int a;
		while((a=fr.read())!=-1){
			sb.append((char) a);
		}
		fr.close();
		return sb.toString().replaceAll(PREFIX_DELIMITER, PREFIX_REPLACEMENT);
	}

	public String readJspHeader(File file) throws DocumentException, FileNotFoundException{
		FileReader fr = new FileReader(file);
		try{
			StringBuilder sb = new StringBuilder();
			int a,b,c=0;
			while((a=fr.read())!=-1){
				if(a=='<'){
					if(((b=fr.read())<65 || b > 122) && b != 47){
						sb.append((char) a);
						sb.append((char) b);
						for(;(a=fr.read())!=-1 && c!=-1;){
							sb.append((char) a);
							if(a=='>' && sb.charAt(sb.length()-2)==sb.charAt(1)){
								c=-1;
							}
						}
						c=0;
					}
				}
			}
			return sb.toString();
		}catch(Exception e){
			System.err.println("Error when trying to read file");
			e.printStackTrace();
			return "";
		}finally{
			try {
				fr.close();
			} catch (IOException e) {
			}
		}
	}
	
//	public int skipJspHeader(File file) throws DocumentException{
//		try{
//			FileReader fr = new FileReader(file);
//			List<Character> input = new ArrayList<Character>();
//			int a,b,c=0;
//			while((a=fr.read())!=-1){
//				if(a=='<'){
//					if(((b=fr.read())<65 || b > 122) && b != 47){
//						input.add((char) a);
//						input.add((char) b);
//						for(;(a=fr.read())!=-1 && c!=-1;){
//							input.add((char) a);
//							if(a=='>' && input.get(input.size()-2)==input.get(1)){
//								c=-1;
//							}
//						}
//						c=0;
//					}
//				}
//			}
//			return input.size();
//		}catch(Exception e){
//			System.err.println("Error when trying to read file");
//			return 0;
//		}
//	}

	public Document readXml() throws DocumentException, IOException, SAXException{
		InputStream strInputStream = new ByteArrayInputStream(body.getBytes("UTF-8"));
		SAXReader reader = new SAXReader(false);
		return reader.read(strInputStream);
	}
}
